package com.gwclh.personnel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gwclh.personnel.bean.ResultVo;
import com.gwclh.personnel.domain.PageDomain;
import com.gwclh.personnel.entity.*;
import com.gwclh.personnel.mapper.StaTransferMapper;
import com.gwclh.personnel.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gwclh.personnel.utils.ShiroUtil;
import com.gwclh.personnel.vo.stationvo.StaTransFerVo;
import com.mchange.v2.beans.BeansUtils;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 岗位调转记录表
 * 服务实现类
 * </p>
 *
 * @author gzhhh
 * @since 2020-07-24
 */
@Service
public class StaTransferServiceImpl extends ServiceImpl<StaTransferMapper, StaTransfer> implements IStaTransferService {
    @Autowired
    IEmployeeService employeeService;

    @Autowired
    StaTransferMapper staTransferMapper;

    @Autowired
    IDepartmentService departmentService;

    @Autowired
    IStationService stationService;
    @Autowired
    IUserService userService;

    @Override
    public ResultVo listStaTrans(PageDomain pageDomain, StaTransFerVo staTransFerVo) {
        Page<StaTransfer> staTransferPage = new Page<>(pageDomain.getPageNum(), pageDomain.getPageSize());
        staTransferPage.addOrder(pageDomain.getOrderItemBy());
        QueryWrapper<StaTransfer> queryWrapper = new QueryWrapper<>();

        if (staTransFerVo.getEmpId() != null) {
            queryWrapper.eq("emp_id", staTransFerVo.getEmpId());
        }
        if (!StringUtils.isEmpty(staTransFerVo.getEmpName())) {
            List<Employee> employees = employeeService.list(new QueryWrapper<Employee>().like("emp_name", staTransFerVo.getEmpName()));
            List<Integer> ids = new ArrayList<>();
            employees.forEach(employee -> {
                ids.add(employee.getId());
            });
            queryWrapper.in("emp_id", ids);
        }
        if (staTransFerVo.getTraType() != null) {
            queryWrapper.eq("tra_type", staTransFerVo.getTraType());
        }

        if (staTransFerVo.getBeginTime() != null) {
            queryWrapper.ge("create_time", staTransFerVo.getBeginTime());
        }
        if (staTransFerVo.getEndTime() != null) {
            queryWrapper.le("create_time", staTransFerVo.getEndTime());
        }
        this.page(staTransferPage, queryWrapper);
        List<StaTransFerVo> staTransFerVos = new ArrayList<>();
        staTransferPage.getRecords().forEach(e -> {
            StaTransFerVo transFerVo = new StaTransFerVo();
            BeanUtils.copyProperties(e, transFerVo);
            System.out.println(e.toString());
            System.out.println(transFerVo.toString());
            Employee employee = employeeService.getById(transFerVo.getEmpId());
            if(employee!=null){
                transFerVo.setEmpName(employee.getEmpName());
            }
            Station preStation = stationService.getById(transFerVo.getPreStaId());
            if(preStation!=null){
                transFerVo.setPreStaName(preStation.getStaName());
            }
            Station newStation = stationService.getById(transFerVo.getNewStaId());
            if(newStation!=null){
                transFerVo.setNewStaName(newStation.getStaName());
            }
            User user = userService.getById(transFerVo.getOperId());
            if(user!=null){
                transFerVo.setOperName(user.getUsername());
            }
            staTransFerVos.add(transFerVo);
        });
        Map<String, Object> result = new HashMap<>();
        result.put("staTransFers", staTransFerVos);
        result.put("total", staTransferPage.getTotal());
        return ResultVo.success(result);
    }

    @Override
    public ResultVo addTran(StaTransfer staTransfer) {
        //获取岗位容量
        Station station = stationService.getById(staTransfer.getNewStaId());
        //获取当前岗位人数
        Integer countStaId = employeeService.count(new QueryWrapper<Employee>().eq("sta_id", staTransfer.getNewStaId()));
        if (countStaId >= station.getStaCapacity()) {
            return ResultVo.fail("岗位人数达到最大值");
        } else {
            staTransfer.setOperId(ShiroUtil.getProfile().getId());
//            System.out.println(staTransfer.toString());
            this.save(staTransfer);
            employeeService.update(new UpdateWrapper<Employee>().eq("id", staTransfer.getEmpId()).set("sta_id", staTransfer.getNewStaId()));
            return ResultVo.success();
        }
    }

    @Override
    public List<Map<String, Object>> getTransData() {
        return staTransferMapper.selectStationTransferData();
    }
}
